package com.linkage.educloud.ah.view.activity.contact;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.ClazzContact;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.config.AppConfig;
import com.linkage.educloud.ah.mvp.contract.IContactDetailPersonalContract;
import com.linkage.educloud.ah.mvp.presenter.ContactDetailPersonalPresenter;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.view.BasicTitleActivity;
import com.linkage.educloud.ah.vnet.PushConsts;
import com.linkage.educloud.ah.widget.CommonDialog;
import com.zzhou.common.library.util.StringUtil;
import com.zzhou.common.library.util.SystemTool;
import com.zzhou.common.library.util.ToastShow;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * <desc>
 * Created by The Moss on 2018/11/29.
 */

public class ContactDetailPersonalActivity extends BasicTitleActivity implements IContactDetailPersonalContract.IView {
    @BindView(R.id.item_avatar)
    CircleImageView mAvatarIv;
    @BindView(R.id.item_name)
    TextView mNameTv;
    @BindView(R.id.item_role)
    TextView mRoleTv;
    @BindView(R.id.item_short_dnTv)
    TextView mShortDnTv;
    @BindView(R.id.item_smsvIv)
    ImageView mSmsvIv;
    @BindView(R.id.item_phonevTv)
    ImageView mPhonevIv;
    @BindView(R.id.item_phoneTv)
    TextView mPhoneTv;
    @BindView(R.id.item_openTv)
    TextView mOpenTv;
    @BindView(R.id.item_smsIv)
    ImageView mSmsIv;
    @BindView(R.id.item_phoneIv)
    ImageView mPhoneIv;
    @BindView(R.id.item_send_message_Ll)
    LinearLayout mSendMessageLl;
    @BindView(R.id.item_invite_tipTv)
    TextView mInviteTipTv;
    @BindView(R.id.item_invite_Ll)
    LinearLayout mInviteLl;

    static String KEY_CONTACT = "key_contact";
    ClazzContact clazzContact;
    private CommonDialog dialog;
    IContactDetailPersonalContract.IPresenter iPresenter;

    public static void luncher(Context context, ClazzContact contact) {
        Intent intent = new Intent(context, ContactDetailPersonalActivity.class);
        intent.putExtra(KEY_CONTACT, contact);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_contacts_persondetail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iPresenter = new ContactDetailPersonalPresenter(this);
        getExtra();
    }

    @OnClick({R.id.item_smsvIv, R.id.item_phonevTv, R.id.item_smsIv, R.id.item_phoneIv, R.id.item_send_message_Ll, R.id.item_invite_Ll})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.item_smsvIv:
                SystemTool.sendSMS(this, clazzContact.getShort_dn());
                break;
            case R.id.item_phonevTv:
                SystemTool.dailTel(this, clazzContact.getShort_dn());
                break;
            case R.id.item_smsIv:
                SystemTool.sendSMS(this, clazzContact.getDn());
                break;
            case R.id.item_phoneIv:
                SystemTool.dailTel(this, clazzContact.getDn());
                break;
            case R.id.item_send_message_Ll:
                if (clazzContact.invite != 2) {// 用户登录过客户端，可以发起聊天，，否者不能聊天
                    ToastShow.show(this, "TA未使用和教育客户端，不能发消息给TA，您可邀请TA使用。");
                    break;
                }
                Long mBuddyId;
                if (clazzContact.getType() == 1) {
                    mBuddyId = Long.parseLong(PushConsts.VID_P + clazzContact.id);
                } else {
                    mBuddyId = Long.parseLong(PushConsts.VID_T + clazzContact.id);
                }
                // TODO: 2018/11/29 跳转到聊天界面
//                Intent inte = VNewChatActivity.getIntent(this
//                        ,mBuddyId
//                        ,clazzContact.getName()
//                        , Consts.ChatType.CHAT_TYPE_SINGLE
//                        ,clazzContact.getType()
//                        ,Consts.MSGLIST_ORDER_SIGLE_CHAT);
//                startActivity(inte);
//                finish();
                break;
            case R.id.item_invite_Ll:
                if (clazzContact.invite == 0) {
                    dialog = new CommonDialog(this, "提示", "确定邀请"
                            + clazzContact.name + "使用和教育客户端吗?", "取消", "确定");

                    dialog.setOkListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // TODO: 2018/11/29 邀请请求
                            iPresenter.sendInvite(clazzContact.id + "", clazzContact.type);
                            dialog.dismiss();
                        }
                    });
                    dialog.setCancelListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (dialog.isShowing()) {
                                dialog.dismiss();
                            }
                        }
                    });
                    dialog.show();
                } else {
                    ToastShow.show(this, "已经被邀请了，请稍后再试~");
                }
                break;
        }
    }

    @Override
    public void onInviteResult(boolean isSuccess) {
        if (isSuccess) {
            clazzContact.invite = 1;
            mInviteTipTv.setText("已邀请");
            mAvatarIv.setImageResource(R.mipmap.ic_default_avatarwarn);
        } else {
            ToastShow.show(this, "邀请失败，请稍微在试~");
        }
    }

    private void getExtra() {
        Bundle bundle = getIntent().getExtras();
        if (bundle == null) {
            return;
        }
        clazzContact = bundle.getParcelable(KEY_CONTACT);
        if (clazzContact == null) {
            return;
        }
        setTitle("人员详情");
        mNameTv.setText(clazzContact.getName());
        mRoleTv.setText(clazzContact.getType() == 1 ? "家长" : "教师");
        if (clazzContact.getInvite() == 2 || StringUtil.isNullOrEmpty(clazzContact.getDn())) {// 已经登陆过客户端不需要邀请或手机号为空,隐藏
            mInviteLl.setVisibility(View.GONE);
        } else {
            mInviteLl.setVisibility(View.VISIBLE);
            if (clazzContact.getInvite() == 1) {
                mInviteTipTv.setText("已邀请");
                mAvatarIv.setImageResource(R.mipmap.ic_default_avatarwarn);
            } else {
                mInviteTipTv.setText("");
                if (clazzContact.getInvite() == 0) {
                    mAvatarIv.setImageResource(R.mipmap.ic_default_avatarwarn);
                } else {
                    ImageLoader.loadAvatar(this, clazzContact.getAvatar_url(), mAvatarIv);
                }
            }
        }
        int netType = SystemTool.getPhoneInfos(this);
        if (StringUtil.isNullOrEmpty(clazzContact.getShort_dn())) {
            mShortDnTv.setText("未开通");
            mShortDnTv.setTextColor(Color.RED);
        } else {
            mShortDnTv.setText(clazzContact.getShort_dn());
            mShortDnTv.setTextColor(Color.BLACK);
            int has_shortdn = AppConfig.getInstance().getInt(AppConfig.ShareKey.KEY_HAS_SHORTDN, 0);
            String clazzid = AppConfig.getInstance().getString(AppConfig.ShareKey.KEY_SHORTDN_CLAZZS, "");
            if (!StringUtil.isNullOrEmpty(clazzid)) {
                String[] clazzs = clazzid.split(",");
                List clazzids = Arrays.asList(clazzs);
                if (SystemTool.YIDONG == netType
                        && has_shortdn == Constants.VClazzType.Vclass
                        && clazzids.contains(clazzContact.classid)) {
                    mPhonevIv.setVisibility(View.VISIBLE);
                    mSmsvIv.setVisibility(View.VISIBLE);
                } else {
                    mPhonevIv.setVisibility(View.GONE);
                    mSmsvIv.setVisibility(View.GONE);
                }
            } else {
                mPhonevIv.setVisibility(View.GONE);
                mSmsvIv.setVisibility(View.GONE);
            }
        }
        if (StringUtil.isNullOrEmpty(clazzContact.getDn())) {
            mPhoneTv.setText("未绑定手机号");
            mPhoneTv.setTextColor(Color.RED);
        } else {
            if (AccountConfig.getLoginAccount().getUserType() == Constants.UserType.TEACHER) {
                mPhoneTv.setText(clazzContact.getDn());
                mPhoneTv.setTextColor(Color.BLACK);
                if (!StringUtil.isNullOrEmpty(clazzContact.getDn())) {
                    mPhoneIv.setVisibility(View.VISIBLE);
                    mSmsIv.setVisibility(View.VISIBLE);
                } else {
                    mPhoneIv.setVisibility(View.GONE);
                    mSmsIv.setVisibility(View.GONE);
                }
            } else {
                if (clazzContact.isopen_phone == 0 && clazzContact.dn.length() == 11) {
                    mPhoneTv.setText(clazzContact.dn.substring(0, 3)
                            + "****"
                            + clazzContact.dn.substring(7,
                            clazzContact.dn.length()));
                    mPhoneIv.setVisibility(View.GONE);
                    mSmsIv.setVisibility(View.GONE);
                    mOpenTv.setVisibility(View.VISIBLE);
                } else {
                    mPhoneTv.setText(clazzContact.dn);
                    mOpenTv.setVisibility(View.GONE);
                    if (StringUtil.isNullOrEmpty(clazzContact.dn)) {
                        mSmsIv.setVisibility(View.GONE);
                        mPhoneIv.setVisibility(View.GONE);
                    } else {
                        mSmsIv.setVisibility(View.VISIBLE);
                        mPhoneIv.setVisibility(View.VISIBLE);
                    }
                }
            }
        }
    }
}
